package com.c2soft.ncmxTrade.controller;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.c2soft.ncmxTrade.util.JsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Controller
@RequestMapping(value = "/mock")
public class MockServerController {
	
	@Value("#{systemProperties['ncmxServer.redirect.url']}")
	private String baseServerUrl;
	@Autowired
	private RestTemplate restTemplate;
	
@RequestMapping(value = "/clientpage",method=RequestMethod.GET)
	
	public @ResponseBody List clientpage(HttpServletRequest req) {
		
		String userId=(String) req.getSession().getAttribute("trdUsrId");
		URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
			    .path("mock/clientpage1")
			    .queryParam("userId", userId)
			    .build()
			    .toUri();
		List response=restTemplate.getForObject(redirectUrl, List.class);
		String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
	}

@RequestMapping(value = "/bank",method=RequestMethod.GET)

public @ResponseBody List bank(HttpServletRequest req) {
	
	String userId=(String) req.getSession().getAttribute("trdUsrId");
	URI redirectUrl= UriComponentsBuilder.fromUriString(baseServerUrl)
		    .path("mock/bank1")
		    .queryParam("userId", userId)
		    .build()
		    .toUri();
	
	
	List response=restTemplate.getForObject(redirectUrl, List.class);
	
	String[] appendArray={"{\"rows\":",response.toString(),"}"};//TODO: clean junk
		return response;
}


	@RequestMapping(value = "/buy", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public String buy() {
		Map<String, String> buyDtlMap = new HashMap<String, String>();
		buyDtlMap.put("insname", "206803");
		buyDtlMap.put("symbol", "GOLD");
		buyDtlMap.put("qt", "6");
		buyDtlMap.put("ql", "6");
		buyDtlMap.put("qw", "2");
		buyDtlMap.put("price", "48000");
		buyDtlMap.put("tp", "11111");
		return JsonUtil.toJson(buyDtlMap); 
	}
	@RequestMapping(value = "/sell", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public String sell() {
		Map<String, String> sellDtlMap = new HashMap<String, String>();
		sellDtlMap.put("insname1", "206803");
		sellDtlMap.put("symbol1", "GOLD");
		sellDtlMap.put("qt1", "6");
		sellDtlMap.put("ql1", "6");
		sellDtlMap.put("qw1", "2");
		sellDtlMap.put("price1", "48000");
		sellDtlMap.put("tp1", "11111");
		return JsonUtil.toJson(sellDtlMap); 
	}
	@RequestMapping(value = "/clientfundview", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public String clientfundview() {
		Map<String, String> clientfundviewDtlMap = new HashMap<String, String>();
		clientfundviewDtlMap.put("id", "C0000");
		clientfundviewDtlMap.put("cname", "Pankaj Kumar Sharma");
		clientfundviewDtlMap.put("mobile", "9876543210");
		clientfundviewDtlMap.put("closeb", "100000");
		clientfundviewDtlMap.put("totm", "500000");
		clientfundviewDtlMap.put("depf", "20000");
		clientfundviewDtlMap.put("utim", "100000");
		clientfundviewDtlMap.put("withf", "10000");
		clientfundviewDtlMap.put("netp", "5000");
		clientfundviewDtlMap.put("balm", "400000");
		clientfundviewDtlMap.put("tot", "115000");
		
		return JsonUtil.toJson(clientfundviewDtlMap); 
	}
	
	@RequestMapping(value = "/tradebook", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json")
	@ResponseBody
	public String tradebook() {
		Map<String, String> tradebookDtlMap = new HashMap<String, String>();
		tradebookDtlMap.put("insname1", "206803");
		tradebookDtlMap.put("symbol1", "GOLD");
		tradebookDtlMap.put("qt1", "6");
		tradebookDtlMap.put("ql1", "6");
		tradebookDtlMap.put("qw1", "2");
		tradebookDtlMap.put("price1", "48000");
		tradebookDtlMap.put("tp1", "");
		return JsonUtil.toJson(tradebookDtlMap); 
}
}